<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="80%" v-dialog-drag append-to-body>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column type="index" label="序号" align="center" width="50"/>
        <el-table-column label="托盘/RFID" align="center" prop="rfidCode" />
        <el-table-column label="物料箱码" align="center" prop="boxCode" />
        <el-table-column label="物料名称" align="center" prop="materialName"/>
        <el-table-column label="产品码" align="center" prop="materialCode"/>
        <el-table-column label="物料代码" align="center" prop="materialModeCode"/>
        <el-table-column label="质检员" align="center" prop="operatorName"/>
        <el-table-column label="所在仓库" align="center" prop="warehouseId" :formatter="warehouseFormat" width="150"/>
        <el-table-column label="质检结果" align="center" prop="qcResult">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.QC_RESULT" :value="scope.row.qcResult"/>
          </template>
        </el-table-column>
        <el-table-column label="关联质检单" align="center" prop="qcOrderCode" width="150"/>
        <el-table-column label="质检时间" align="center" prop="createTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>、
    </el-dialog>
  </div>
</template>

<script>
import * as QcHistoryApi from '@/api/hs/qchistory';
import {getWarehouseSimpleList} from "@/api/hs/warehouse";

export default {
  name: "QcHistory",
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 物料质检历史记录列表
      list: [],
      warehouseList: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialCode: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 打开弹窗 */
    async open(materialCode) {
      this.dialogVisible = true;
      this.title = "质检历史记录";
      await this.getWarehouseList();
      await this.getList(materialCode)
    },
    /** 查询列表 */
    async getList(materialCode) {
      try {
        this.loading = true;
        this.queryParams.materialCode = materialCode;
        const res = await QcHistoryApi.getQcHistoryPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 查询仓库列表 */
    async getWarehouseList() {
      getWarehouseSimpleList().then(res => {
        this.warehouseList = res.data;
      })
    },
    // 仓库名称展示
    warehouseFormat(row, column) {
      if (!row.warehouseId) {
        return '未设置';
      }
      for (const warehouse of this.warehouseList) {
        if (row.warehouseId === warehouse.id) {
          return warehouse.name;
        }
      }
      return '未知【' + row.warehouseId + '】';
    },
  }
};
</script>
